
package com.companymodule.dao;
import com.companymodule.bean.*;
import java.sql.*;
import java.util.*;
import util.*;
public class TbCompanyDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbCompany tbCompany=null;
    private List<TbCompany> tbCompanyList=null;

    public boolean add(TbCompany inTbCompany){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_Company(CompanyCode,CompanyTypeId,CompanyName,ParentCompanyName,CompanyAddress,CompanyTelphone,CompanyMobile,CompanyManCount,CompanyContactList,CreditRatingId,CompanyMemo) values(?,?,?,?,?,?,?,?,?,?,?)";
        pt = conn.prepareStatement(sql);
        
        pt.setString(1,inTbCompany.getCompanyCode());
        if(inTbCompany.getCompanyTypeId()=="0")pt.setString(2, "");
        else
        pt.setString(2,inTbCompany.getCompanyTypeId());
        pt.setString(3,inTbCompany.getCompanyName());
        pt.setString(4,inTbCompany.getParentCompanyName());
        pt.setString(5,inTbCompany.getCompanyAddress());
        pt.setString(6,inTbCompany.getCompanyTelphone());
        pt.setString(7,inTbCompany.getCompanyMobile());
        pt.setInt(8,inTbCompany.getCompanyManCount());
        pt.setString(9,inTbCompany.getCompanyContactList());
        if(inTbCompany.getCreditRatingId()==0)
        	pt.setNull(10, Types.INTEGER);
        else
            pt.setInt(10,inTbCompany.getCreditRatingId());
        pt.setString(11,inTbCompany.getCompanyMemo());
        //pt.setString(13,inTbCompany.getCreateTime());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return flag;
    }
    public boolean del(int inCompanyId){
        return UtilDAO.del("Tb_Company", "CompanyId", inCompanyId);
    }
    public boolean edit(TbCompany inTbCompany){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_Company set CompanyCode=?,CompanyTypeId=?,CompanyName=?,ParentCompanyName=?,CompanyAddress=?,CompanyTelphone=?,CompanyMobile=?,CompanyCreateTime=?,CompanyManCount=?,CompanyContactList=?,CreditRatingId=?,CompanyMemo=?,CreateTime=getdate() where CompanyId=?";
        pt = conn.prepareStatement(sql);
        
        pt.setString(1,inTbCompany.getCompanyCode());
        if(inTbCompany.getCompanyTypeId()=="0")pt.setString(2, "");
        else
        pt.setString(2,inTbCompany.getCompanyTypeId());
        pt.setString(3,inTbCompany.getCompanyName());
        pt.setString(4,inTbCompany.getParentCompanyName());
        pt.setString(5,inTbCompany.getCompanyAddress());
        pt.setString(6,inTbCompany.getCompanyTelphone());
        pt.setString(7,inTbCompany.getCompanyMobile());
        pt.setString(8,inTbCompany.getCompanyCreateTime());
        pt.setInt(9,inTbCompany.getCompanyManCount());
        pt.setString(10,inTbCompany.getCompanyContactList());
        if(inTbCompany.getCreditRatingId()==0)pt.setNull(11, Types.INTEGER);
        else
        pt.setInt(11,inTbCompany.getCreditRatingId());
        pt.setString(12,inTbCompany.getCompanyMemo());
        pt.setInt(13,inTbCompany.getCompanyId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
            
        }
        return flag;
    }
    public TbCompany getTbCompanyByCompanyId(int inCompanyId) {
        tbCompany=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Company where CompanyId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inCompanyId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbCompany == null)tbCompany = getTbCompany(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return tbCompany;
    }
    public List<TbCompany> getTbCompanyAll() {
        tbCompanyList=new ArrayList<TbCompany>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Company";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbCompanyList.add(getTbCompany(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbCompanyList;
    }
    /**
     * 
     *@author qingyu zhang
     *@function:
     * @return
     *2011-5-13
     *下午03:40:00
     *ProjectManager
     *com.companymodule.dao
     *Map<Integer,TbCompany>
     *//*
    public Map<Integer,TbCompany> getTbCompany(){
    	mapCompany = new HashMap<Integer,TbCompany>();
    	 try{
             conn = ConnectDB.getSqlServerConnection();
             sql = "select * from Tb_Company";
             pt = conn.prepareStatement(sql);
             rs = pt.executeQuery();
             while (rs.next()) {
            	 mapCompany.put(getTbCompany(rs).getCompanyId(), getTbCompany(rs));
             }
         }catch(Exception e){
             e.printStackTrace();
         }finally {
         	ConnectDB.closeSqlConnection();
         }
         return mapCompany;
    }*/
    public List<TbCompany> getTbCompanyAll(VskPage vp) {
        tbCompanyList=new ArrayList<TbCompany>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Company";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_Company", "CompanyId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbCompanyList.add(getTbCompany(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return tbCompanyList;
    }
    public TbCompany getTbCompany(ResultSet rs) throws Exception{
        TbCompany tbCompany = new TbCompany();
        tbCompany.setCompanyId(rs.getInt("CompanyId"));
        tbCompany.setCompanyCode(rs.getString("CompanyCode"));
        tbCompany.setCompanyTypeId(rs.getString("CompanyTypeId"));
        tbCompany.setCompanyName(rs.getString("CompanyName"));
        tbCompany.setParentCompanyName(rs.getString("ParentCompanyName"));
        tbCompany.setCompanyAddress(rs.getString("CompanyAddress"));
        tbCompany.setCompanyTelphone(rs.getString("CompanyTelphone"));
        tbCompany.setCompanyMobile(rs.getString("CompanyMobile"));
        tbCompany.setCompanyCreateTime(rs.getString("CompanyCreateTime"));
        tbCompany.setCompanyManCount(rs.getInt("CompanyManCount"));
        tbCompany.setCompanyContactList(rs.getString("CompanyContactList"));
        tbCompany.setCreditRatingId(rs.getInt("CreditRatingId"));
        tbCompany.setCompanyMemo(rs.getString("CompanyMemo"));
        tbCompany.setCreateTime(rs.getString("CreateTime"));
        return tbCompany;
    }
}
